<?php

/**
 * 自动生成 WxEventShakeUserService
 * 
 * Apps/Common/Service/WxEventShakeUserService.class.php
 * Zix @ 2015-09-03
 */

namespace Common\Service;

class WxEventShakeUserService {

  private static $instance;

  public static function instance() {
    if (self::$instance == null) {
      $instance = new WxEventShakeUserService();
    }

    return $instance;
  }

  function get_default_row() {
    return array(
      'id' => '',
      'mer_id' => '',
      'user_id' => '',
      'award_id' => '',
      'award_value' => '0.00',
      'award_status' => '0',
      'create_time' => date('Y-m-d H:i:s'),
    );
  }

  function get_by_id($id) {
    $WxEventShakeUser = M('WxEventShakeUser');
    $data = $WxEventShakeUser->find($id);
    return $data ? $data : array();
  }

  function get_by_cond($config) {
    $default = array(
      'user_id' => '',
      'event_id' => '',
      'mer_id' => '',
      'page' => 1,
      'page_size' => 20 ,
      'without_no_award' => FALSE ,
      'status' => '',
      'count' => FALSE,
      'order' => 'DESC',
      'sort' => 'id',
    );

    $config = extend($config, $default);

    $WxEventShakeUser = M('WxEventShakeUser');

    $where = array();

    if (!empty($config['status'])) {
      $where['esu.status'] = $config['status'];
    }
    
    if (!empty($config['user_id'])) {
      $where['esu.user_id'] = $config['user_id'];
    }
    
    if (!empty($config['event_id'])) {
      $where['esu.event_id'] = $config['event_id'];
    }
    
    if ( $config['without_no_award'] ) {
      $where['a.type'] = array('neq' , 99) ;
    }

    if ($config['count']) {
      return $WxEventShakeUser
        ->alias('esu')
        ->join( 'wx_event_shake_award a on a.id = esu.award_id' , 'left' )
        ->where($where)->count();
    } else {
      $order = 'esu.' . $config['sort'] . ' ' . $config['order'];
      $limit = ($config['page'] - 1 ) * $config['page_size'] . ' , ' . $config['page_size'];
      $data = $WxEventShakeUser
        ->alias('esu')
        ->field('esu.* , a.type , a.name , a.icon , a.sn_url , a.amount')
        ->join( 'wx_event_shake_award a on a.id = esu.award_id' , 'left' )
        ->where($where)
        ->limit($limit)->order($order)->select();
      //echo $WxEventShakeUser->_sql();
      return $data ? $data : array();
    }
  }

  function create($data, $is_ajax = true) {
    $WxEventShakeUser = M('WxEventShakeUser');
    $ret = $WxEventShakeUser->add($data);
    if ($is_ajax) {
      if ($ret) {
        return ajax_arr('添加成功', TRUE, array(
          'id' => $ret
        ));
      } else {
        return ajax_arr('添加失败', FALSE);
      }
    } else {
      if ($ret) {
        return $ret;
      } else {
        return 0;
      }
    }
  }

  function update($id, $data) {
    $WxEventShakeUser = M('WxEventShakeUser');

    $ret = $WxEventShakeUser->where("id = %d", $id)->save($data);
    if ($ret) {
      return ajax_arr('编辑成功', TRUE);
    } else {
      return ajax_arr('编辑失败', FALSE);
    }
  }

  function delete($ids) {
    $WxEventShakeUser = M('WxEventShakeUser');
    $ret = $WxEventShakeUser->delete($ids);
    if ($ret == 0) {
      return ajax_arr('未删除任何数据', FALSE);
    } else if (!$ret) {
      return ajax_arr('删除失败', FALSE);
    } else {
      return ajax_arr('删除' . $ret . '行数据', TRUE);
    }
  }
  
  function count_total_by_user( $event_id , $user_id  ) {
    $where = array(
      'event_id' => $event_id,
      'user_id' => $user_id,
    );
    return $this->_count_times($where);
  }
  
  function count_today_by_user( $event_id , $user_id ) {
    $today = date('Y-m-d');
    $where = array(
      'event_id' => $event_id,
      'user_id' => $user_id,
      '_string' => "DATE_FORMAT( create_time , '%Y-%m-%d') = '$today' ",
    );
    return $this->_count_times($where);
  }
  
  function count_tomonth_by_user( $event_id , $user_id ) {
    $tomonth = date('Y-m');
    $where = array(
      'event_id' => $event_id,
      'user_id' => $user_id,
      '_string' => "DATE_FORMAT( create_time , '%Y-%m') = '$tomonth' ",
    );
    return $this->_count_times($where);
  }
  
  function count_award_by_user( $event_id , $user_id , $award_id) {
    $where = array(
      'event_id' => $event_id,
      'user_id' => $user_id,
      'award_id' => $award_id ,
    );
    return $this->_count_times($where);
  }
  
  function _count_times( $where ) {
    $WxEventShakeUser = M('WxEventShakeUser');
    $field = ' count( user_id ) as amount';
    $data = $WxEventShakeUser->field( $field )->where($where)->group('user_id')->find(); 
    //echo $WxEventShakeUser->_sql();
    return $data;
  }

}
